package servlet;

import bean.Book;
import dao.BookDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ListBookServlet", value = "/ListBookServlet")
public class ListBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        BookDao bookDao = new BookDao();
        List<Book> bookArrayList = null;
        try {
            bookArrayList = bookDao.findAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        request.setAttribute("rsList",bookArrayList);
        request.setCharacterEncoding("utf-8");

        request.getRequestDispatcher("/list.jsp").forward(request,response);
        for (int i=0;i<bookArrayList.size();i++){
            response.getWriter().println(bookArrayList.get(i).getBookName());
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
